package cn.ang.app.controller;

import cn.ang.app.pojo.AppInfo;
import cn.ang.app.pojo.extend.AppInfoExt;
import cn.ang.app.service.AppInfoService;
import cn.ang.app.tools.Pagelimit;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/appInfo")
public class AppInfoController {

    @Resource(name = "appInfoServiceImpl")
    private AppInfoService appInfoService;

    @RequestMapping("/showAppInfo")
    public @ResponseBody Object showAppInfo(AppInfoExt appInfo, Integer pageNo, Integer pageSize) {
        Pagelimit<AppInfoExt> pagelimit = appInfoService.showAppInfo(appInfo, pageNo, pageSize);
        return pagelimit;
    }

    @RequestMapping("/deleteAppInfo")
    public @ResponseBody boolean deleteAppInfo( Long id ) {
        boolean boo = appInfoService.deleteAppInfo(id);
        return boo;
    }

    @RequestMapping("/addAppInfo")
    public @ResponseBody boolean addAppInfo(AppInfo appInfo, MultipartFile logoPath, HttpServletRequest request) throws IOException {
        String filename = logoPath.getOriginalFilename();
        System.out.println(filename);
        String suffix= filename.substring(filename.lastIndexOf('.'));

        String path= request.getServletContext().getRealPath("resources/images");
        String filename1= path+UUID.randomUUID()+suffix;

        FileUtils.copyInputStreamToFile(logoPath.getInputStream(),new File(path, UUID.randomUUID()+suffix));

        appInfo.setLogoPicPath(filename1);
        appInfo.setLogoLocPath(filename1);
        boolean boo = appInfoService.insertAppInfo(appInfo);

        return boo;
    }
}
